package ntut.andengine.game;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.entity.modifier.AlphaModifier;
import org.anddev.andengine.entity.modifier.DelayModifier;
import org.anddev.andengine.entity.modifier.LoopEntityModifier;
import org.anddev.andengine.entity.modifier.MoveModifier;
import org.anddev.andengine.entity.modifier.ParallelEntityModifier;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.util.Log;

public class GameInit extends GameScene {

	private static final int CAMERA_WIDTH = 480;
	//private static final int CAMERA_HEIGHT = 800;
	
	private TextureRegion mGameTitleTextureRegion;
	private TextureRegion mGameInitTextureRegion;
	private TextureRegion mSpotLightTextureRegion;
	private TextureRegion mStartButtonTextureRegion;
	private TextureRegion mAboutTextureRegion;

	@Override
	public void onLoadResources() {
		super.onLoadResources();
		
		BitmapTexture bitmapTexture = new BitmapTexture(512, 512, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		
		this.mGameTitleTextureRegion = BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "game_title.png", 0, 0);
		this.mStartButtonTextureRegion = BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "start_button.png", 0, 56);
		this.mGameInitTextureRegion = BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "game_init.png", 0, 100);
		this.mSpotLightTextureRegion = BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "spot_light.png", 0, 400);
		this.mAboutTextureRegion = BitmapTextureRegionFactory.createFromAsset(bitmapTexture, this, "about.png", 80, 400);
		
		mEngine.getTextureManager().loadTexture(bitmapTexture);	
	}

	@Override
	public Scene onLoadScene() {
		final int centerX = (CAMERA_WIDTH - this.mStartButtonTextureRegion.getWidth()) / 2;
		//final int centerY = (CAMERA_HEIGHT - this.mStartButtonTextureRegion.getHeight()) / 2;
		
		final Sprite gameTitle = new Sprite(240 - this.mGameTitleTextureRegion.getWidth()/2, 100 - this.mGameTitleTextureRegion.getHeight()/2, this.mGameTitleTextureRegion);
		final Sprite gameInit = new Sprite(0, 200, this.mGameInitTextureRegion);
		gameInit.setColor(0, 0, 0);
		final Sprite spotLight = new Sprite(90, 205, this.mSpotLightTextureRegion) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
				this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
				Log.d("pos", String.valueOf(getX())+","+String.valueOf(getY()));
				return true;
			}
		};
		spotLight.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		
		final Sprite starButton = new Sprite(centerX, 600 - this.mStartButtonTextureRegion.getHeight()/2, this.mStartButtonTextureRegion){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameInit.this, GameLevelChoose.class);//Level11
				}
				return true;
			}			
		};
		
		final Sprite aboutButton = new Sprite(centerX, 700 - this.mAboutTextureRegion.getHeight()/2, this.mAboutTextureRegion){
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if(pSceneTouchEvent.isActionDown()){
					startActivity(GameInit.this, GameAbout.class);//Level11
				}
				return true;
			}
		};
		
		spotLight.registerEntityModifier(new LoopEntityModifier(new SequenceEntityModifier(
				new DelayModifier(0.5f),
				new MoveModifier(1.5f, 90, 145, 205, 300),
				new DelayModifier(0.2f),
				new ScaleModifier(0.3f, 1, 2),
				new DelayModifier(0.5f),
				new MoveModifier(1, 145-spotLight.getWidth()/2, 80-spotLight.getWidth()/2, 300-spotLight.getHeight()/2, 445-spotLight.getHeight()/2),
				new DelayModifier(1f),
				new MoveModifier(0.5f, 80-spotLight.getHeight()/2, 270-spotLight.getHeight()/2, 445-spotLight.getHeight()/2, 445-spotLight.getHeight()/2),
				new DelayModifier(1f),
				new MoveModifier(2f, 80-spotLight.getHeight()/2, 270-spotLight.getHeight()/2, 445-spotLight.getHeight()/2, 445-spotLight.getHeight()/2),
				new DelayModifier(1.5f),
				new MoveModifier(1.5f, 270-spotLight.getHeight()/2, 300-spotLight.getHeight()/2, 445-spotLight.getHeight()/2, 330-spotLight.getHeight()/2),
				new DelayModifier(0.5f),
				new MoveModifier(1f, 300-spotLight.getHeight()/2, 450-spotLight.getHeight()/2, 330-spotLight.getHeight()/2, 450-spotLight.getHeight()/2),
				new ParallelEntityModifier(
						new MoveModifier(2f, 450-spotLight.getHeight()/2, 450-spotLight.getHeight()/2, 450-spotLight.getHeight()/2, 320-spotLight.getHeight()/2),
						new ScaleModifier(2f, 2, 4)
						),
				new DelayModifier(2f),
				new MoveModifier(0.5f, 450-spotLight.getHeight()/2, 30-spotLight.getHeight()/2, 320-spotLight.getHeight()/2, 320-spotLight.getHeight()/2),
				new ParallelEntityModifier(
						new MoveModifier(0.5f, 30-spotLight.getHeight()/2, 240-spotLight.getHeight()/2, 320-spotLight.getHeight()/2, 400-spotLight.getHeight()/2),
						new ScaleModifier(0.5f, 4, 6)
						),
				new AlphaModifier(3, 1, 0),
				new ScaleModifier(1f, 6, 1),
				new AlphaModifier(1f, 0, 1)
        )));
		
		gameInit.registerEntityModifier(new LoopEntityModifier(new SequenceEntityModifier(
				new DelayModifier(15f),
				new AlphaModifier(1.5f, 1, 0),
				new DelayModifier(5.5f),
				new AlphaModifier(1f, 0, 1)
				)));
		
		mScene = new Scene();
		mScene.attachChild(gameTitle);
		mScene.attachChild(starButton);
		mScene.attachChild(aboutButton);
		mScene.attachChild(spotLight);
		//mScene.registerTouchArea(spotLight);
		mScene.setTouchAreaBindingEnabled(true);
		mScene.attachChild(gameInit);			
		mScene.registerTouchArea(starButton);
		mScene.registerTouchArea(aboutButton);

		return mScene;
	}

}
